package com.atom.app.mvvm

import com.atom.app.mvvm.db.ImageDao
import com.atom.app.mvvm.db.KingDatabase
import com.atom.app.mvvm.net.RetrofitFactory
import com.atom.module.mvvm.base.ApiModel
import com.tencent.mmkv.MMKV

object AppModel : ApiModel {

    val imageDao: ImageDao = KingDatabase.getDatabase(App.getApp()).imageDao()

    val netApi: AppNetApi

    val mmkv: MMKV = MMKV.defaultMMKV()

    init {
        val createRetrofit =
            RetrofitFactory.createRetrofit(Contacts.baseUrl, RetrofitFactory.createClient())
        netApi = createRetrofit.create(AppNetApi::class.java)
    }

    override fun onCleared() {

    }

    fun tokenClean() {
        mmkv.removeValueForKey(Contacts.net_access_token)
        mmkv.removeValueForKey(Contacts.net_refresh_token)
    }

    fun tokenAccessSave(token: String?) {
        if (token.isNullOrEmpty()) {
            return
        }
        mmkv.encode(Contacts.net_access_token, token)
    }

    fun tokenRefreshSave(token: String?) {
        if (token.isNullOrEmpty()) {
            return
        }
        mmkv.encode(Contacts.net_refresh_token, token)
    }

    fun tokenRefreshGet(): String? {
        return mmkv.decodeString(Contacts.net_refresh_token)
    }

    fun tokenAccessGet(): String? {
        return mmkv.decodeString(Contacts.net_access_token)
    }

}
